package com.dream.system.action;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.dream.common.JsonString;
import com.dream.common.Msg;
import com.dream.common.SuperController;
import com.dream.model.SysUser;
import com.dream.model.Sys_Class;
import com.dream.model.Sys_Lable;
import com.dream.model.Sys_Room;
import com.dream.system.service.ISysClass;
import com.dream.system.service.ISysUser;

@Controller
@RequestMapping("/edu/sysClassAction.do")
@SuppressWarnings("all")
public class SysClassAction extends SuperController {
	@Autowired
	private ISysClass sysClassServiceImpl;

	/**
	 * 增加集团
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 * @throws Exception
	 */
	@RequestMapping(params = "method=addClass")
	public void addClass(Sys_Class model, HttpServletResponse response, HttpServletRequest request) throws IOException {

		model.setName(request.getParameter("cname"));

		Sys_Class scamcodeClass = this.sysClassServiceImpl.getSameClassCode(model.getCpassword());
		if (scamcodeClass != null) {
			Msg msg = this.getMsg("0", "已经有相同的班级密码，请更换其他密码", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}
		int temp = this.sysClassServiceImpl.addSysClass(model);
		if (temp == 1) {
			Msg msg = this.getMsg("1", "操作成功!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		} else if (temp == 2) {
			Msg msg = this.getMsg("0", "已经存在当前级别中存在相同名称的节点!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		} else {
			Msg msg = this.getMsg("0", "操作失败!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		}
	}

	/**
	 * 查询集团树
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=getAllClass")
	public void getAllClass(Sys_Class model, HttpServletResponse response, HttpServletRequest request) throws IOException {

		response.setCharacterEncoding("utf-8");
		Map map = this.sysClassServiceImpl.getAllClass();
		// Msg msg = this.getMsg("0", "操作成功!",
		// JSONObject.fromObject(map).toString());
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(JsonString.sys_class);
		response.getWriter().write(JSONArray.fromObject(map, jsonConfig).toString());
		response.getWriter().close();
		response.getWriter().flush();

	}

	/**
	 * 删除集团树
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=deleteClass")
	public void deleteClass(Sys_Class model, HttpServletResponse response, HttpServletRequest request) throws IOException {

		int map = this.sysClassServiceImpl.deleteClass(model);
		Msg msg = this.getMsg("1", "操作成功!", null);
		response.getWriter().write(JSONObject.fromObject(msg).toString());
		response.getWriter().close();
		response.getWriter().flush();

	}

	/**
	 * 删除集团树
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=updateClass")
	public void updateClass(Sys_Class model, HttpServletResponse response, HttpServletRequest request) throws IOException {
		model.setName(request.getParameter("cname"));
		int map = this.sysClassServiceImpl.updateClass(model);
		Msg msg = this.getMsg("1", "操作成功!", null);
		response.getWriter().write(JSONObject.fromObject(msg).toString());
		response.getWriter().close();
		response.getWriter().flush();

	}

	/**
	 * 设置集团班级的教育归属问题
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=addTeacherAddClass")
	public void addTeacherAddClass(Sys_Class model, HttpServletResponse response, HttpServletRequest request) throws IOException {
		String arr = request.getParameter("arr");
		String teacherId = request.getParameter("teacherId");
		List<Sys_Class> list = (List<Sys_Class>) JSONArray.toCollection(JSONArray.fromObject(arr), Sys_Class.class);
		int map = this.sysClassServiceImpl.addTeacherAddClass(list, teacherId);
		if (map == 1) {
			Msg msg = this.getMsg("1", "操作成功!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		} else {
			Msg msg = this.getMsg("0", "操作失败!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		}

	}

	/**
	 * 更新学校作业时间
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=updateClassLreanTime")
	public void updateClassLreanTime(Sys_Class model, HttpServletResponse response, HttpServletRequest request) throws IOException {

		int map = this.sysClassServiceImpl.updateClassLreanTime(model);
		if (map == 1) {
			Msg msg = this.getMsg("1", "操作成功!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		} else {
			Msg msg = this.getMsg("0", "操作失败!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		}

	}

	/**
	 * 更新学校作业时间
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=getClassLreanTime")
	public void getClassLreanTime(Sys_Class model, HttpServletResponse response, HttpServletRequest request) throws IOException {

		Sys_Class map = this.sysClassServiceImpl.getClassLreanTime(model);
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setExcludes(JsonString.sys_class);
		response.getWriter().write(JSONObject.fromObject(map, jsonConfig).toString());
		response.getWriter().close();
		response.getWriter().flush();

	}

	/**
	 * 老师增加班级的教室
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=addClass_Room")
	public void addClass_Room(Sys_Room model, HttpServletResponse response, HttpServletRequest request) throws IOException {

		SysUser sysUser = (SysUser) request.getSession().getAttribute("sysUser");

		String sys_Class_table_id = request.getParameter("sys_Class_table_id");
		if (sysUser == null) {
			System.out.println("操作失败!没有找到对应的老师");
			Msg msg = this.getMsg("0", "操作失败!没有找到对应的老师", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}
		if (null == sys_Class_table_id || "".equals(sys_Class_table_id)) {
			System.out.println("操作失败!没有找到对应的班级");
			Msg msg = this.getMsg("0", "操作失败!没有找到对应的班级", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}
		Sys_Class sc = new Sys_Class();
		sc.setTable_id(sys_Class_table_id);
		model.setSys_Class(sc);
		model.setSysUser(sysUser);
		String arr = request.getParameter("arr");
		if (null == arr || "".equals(arr)) {
			Msg msg = this.getMsg("0", "操作失败，教室中没有座位!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
			return;
		}

		int map = this.sysClassServiceImpl.addClass_Room(model, arr);
		if (map == 1) {
			Msg msg = this.getMsg("1", "操作成功!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		} else {
			Msg msg = this.getMsg("0", "操作失败!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		}

	}

	/**
	 * 得到学生当前老师的，在同一班的教室
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=getStudent_Teacher_Room")
	public void getStudent_Teacher_Room(SysUser teacher, HttpServletResponse response, HttpServletRequest request) throws IOException {

		SysUser sysUser = (SysUser) request.getSession().getAttribute("sysUser");
		String sys_class_table_id = request.getParameter("sys_class_table_id");
		String year = request.getParameter("year");
		Map sr = this.sysClassServiceImpl.getStudent_Teacher_Room(sysUser, teacher, sys_class_table_id, year);
		JsonConfig jc = new JsonConfig();
		jc.setExcludes(JsonString.sys_room);
		JSONObject jonArray = JSONObject.fromObject(sr, jc);
		response.getWriter().write(jonArray.toString());
		response.getWriter().close();
		response.getWriter().flush();

	}

	/**
	 * 得到老师选择的班级的教室，指的是当前激活的教室
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=getTeacher_Room_C")
	public void getTeacher_Room_C(SysUser teacher, HttpServletResponse response, HttpServletRequest request) throws IOException {

		SysUser sysUser = (SysUser) request.getSession().getAttribute("sysUser");
		String sys_class_table_id = request.getParameter("sys_class_table_id");
		String year = request.getParameter("year");
		// 由于这的session就是老师了，所以在每个参数中，直接传入老师就可以，
		Map sr = this.sysClassServiceImpl.getStudent_Teacher_Room(sysUser, sysUser, sys_class_table_id, year);
		JsonConfig jc = new JsonConfig();
		jc.setExcludes(JsonString.sys_room);
		JSONObject jonArray = JSONObject.fromObject(sr, jc);
		response.getWriter().write(jonArray.toString());
		response.getWriter().close();
		response.getWriter().flush();

	}

	/**
	 * 得到学生当前老师的，在同一班的教室
	 * 
	 * @param model
	 * @param response
	 * @param request
	 * @throws IOException
	 */
	@RequestMapping(params = "method=addStudentRoomOption")
	public void addStudentRoomOption(Sys_Room model, HttpServletResponse response, HttpServletRequest request) throws IOException {

		SysUser sysUser = (SysUser) request.getSession().getAttribute("sysUser");
		if (null == sysUser) {
			System.out.println("----------------没有用户-----------------------");
			Msg msg = this.getMsg("0", "操作失败!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		}
		int map = this.sysClassServiceImpl.addStudentRoomOption(model, sysUser);
		if (map == 1) {
			Msg msg = this.getMsg("1", "操作成功!", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		} else {
			Msg msg = this.getMsg("0", "对不起，此位置已经有人抢先一步了，请另选", null);
			response.getWriter().write(JSONObject.fromObject(msg).toString());
			response.getWriter().close();
			response.getWriter().flush();
		}
	}

	

}
